package com.smartEdu.module.schedule;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Tag_Tips;
//import utils.DateUtils;
import com.smartEdu.module.schedule.GobalConstant;
import com.smartEdu.utils.IOUtils;

public class Login {
	//private static String stuNumber="";		//学号
	//private static String stuName="";	
	public static String Cookie="";		//Cookie
	private String loginUrl = GobalConstant.LOGIN_URL;
	private String mainUrl = GobalConstant.MAIN_URL;
	//private String userStateN = GobalConstant.USER_STATE_N;
	//private String userStateY = GobalConstant.USER_STATE_Y;
	
	/*
	 * 登陆功能
	 * @param stuNumber
	 * @param password
	 * @param return
	 * throws Exception
	 * throws UnsupportedOperationException
	 * */
	public boolean login(String stuNumber, String password)
			throws  Exception{
		//this.stuNumber = stuNumber;
		//获取Cookie
		HttpGet loginGet = new HttpGet(mainUrl);
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse responseCookie = client.execute(loginGet);
		Cookie = responseCookie.getFirstHeader("set-Cookie").getValue(); 
		//创建登陆请求的Post请求
		HttpPost loginPost = new HttpPost(loginUrl);
		//带上第一次请求的Cookie
		loginPost.setHeader("Cookie",Cookie);
		//封装post提交参数
		List<NameValuePair> nameValuePairLogin =
			new ArrayList<NameValuePair>();
		
		String encodeString = EncodeToLogin.getEncodeString(stuNumber)+"%%%"+EncodeToLogin.getEncodeString(password);
		System.err.println(encodeString);
		nameValuePairLogin.add(new BasicNameValuePair("encoded",encodeString));	//隐藏表单值(验证码)
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairLogin, "utf-8");
		loginPost.setEntity(entity);
		HttpResponse responseLogin = client.execute(loginPost);
		//判断数据是否提交成功，成功返回302
		if(responseLogin.getStatusLine().getStatusCode() == 302){
			//如果提交成功，带着Cookie请求重定向的main页面，并获取学生姓名
			HttpGet mainGet = new HttpGet(mainUrl + stuNumber);
			mainGet.setHeader("Cookie",Cookie);
			HttpResponse responseMain = client.execute(mainGet);
			InputStream is = responseMain.getEntity().getContent();
			String html = "";
			try{
				html = IOUtils.getHtml(is,"utf-8");
			}catch(Exception e){
				System.out.println("解析html失败！");
				e.printStackTrace();
			}
			System.out.println("登陆成功！欢迎您");
			client.close();
			return true;
		}else{
			System.out.println("登陆失败！");
		}
		return false;
	}
	
	public static void main(String[] args){
		Login jw = new Login();
		jw.loginAuth("140202021029","liangruping0429");
		MarkInquiry mk = new MarkInquiry();
		CourseInquiry cs = new CourseInquiry();
		CalenderInquiry caln = new CalenderInquiry();
//		DateUtils da = new DateUtils();
		System.out.println("模拟登陆教务系统");
		try{
//			jw.login("130201031035", "abc123abc");
//			jw.login("140202021029", "liangruping0429");
			//测试周数
//			System.out.println("这周是本学期的第" + da.getWeek() + "周");
//			System.out.println("--成绩查询--");
//			mk.queryStuMark();
			System.out.println("--学生个人课表查询");
			cs.queryStuCourse("2016-2017-2");
//			System.out.println("--校历查询");
//			caln.queryCalender();
//			System.out.println("--等级成绩查询");
//			mk.queryLevlMark();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 验证账号密码正确性
	 * @param loginnname
	 * @param passwd
	 * @return
	 */
	public boolean loginAuth(String loginnname, String passwd) {
		boolean loginAuthState = false;
		try{
			loginAuthState = login(loginnname, passwd);
		}
		catch(Exception e) {
			LogCvt.error("账号密码错误",e);
		}
		return loginAuthState;
	}
}
